package com.zhupf.binaryTree;

/**
 * @author zhupf
 * @date 2023年11月28日 9:42
 * @Description
 *
 * 108. 将有序数组转换为二叉搜索树
 */
public class SortedArrayToBST {

    public static void main(String[] args) {
        int[] nums = {-10,-3,0,5,9};
        SortedArrayToBST sortedArrayToBST = new SortedArrayToBST();
        TreeNode treeNode = sortedArrayToBST.sortedArrayToBST(nums);
        System.out.println(treeNode);
    }

    public TreeNode sortedArrayToBST(int[] nums) {
       return sort(nums,0,nums.length-1);
    }

    public TreeNode sort(int[] nums,int start,int end) {
        if(end < start){
            return null;
        }
        if(end == start){
            return new TreeNode(nums[start]);
        }
        int mid = (end+start)/2;
        return new TreeNode(nums[mid],sort(nums,start,mid-1),sort(nums,mid+1,end));
    }


}
